package com.opensource.component.query.script;

import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

/**
 * @author ZonLen since on 2021/12/27 下午4:36
 */
public enum AggregationFunction {

  SIMPLE(" %s %s "),
  COUNT(" COUNT(%s) %s "),
  COUNT_DISTINCT(" COUNT(DISTINCT %s) %s "),
  SUM(" SUM(%s) %s "),
  MIN(" MIN(%s) %s "),
  MAX(" MAX(%s) %s ");

  private final String function;

  AggregationFunction(String function) {
    this.function = function;
  }

  public String format(String column, String alias) {
    return String.format(function, column,
        StringUtils.isBlank(alias) ? Constants.EMPTY : Constants.AS + alias);
  }
}
